package com.bh.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bh.common.utils.DateUtils;
import com.bh.common.utils.SecurityUtils;
import com.bh.common.utils.StringUtils;
import com.bh.wms.domain.WmsWarehouseLocation;
import com.bh.wms.domain.WmsWarehouseZone;
import com.bh.wms.mapper.WmsWarehouseLocationMapper;
import com.bh.wms.mapper.WmsWarehouseZoneMapper;
import com.bh.wms.service.IWmsWarehouseZoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 库区服务实现
 *
 * @author GPT
 */
@Service
public class WmsWarehouseZoneServiceImpl extends ServiceImpl<WmsWarehouseZoneMapper, WmsWarehouseZone> implements IWmsWarehouseZoneService {

    @Autowired
    private WmsWarehouseLocationMapper warehouseLocationMapper;

    @Override
    public WmsWarehouseZone selectWmsWarehouseZoneById(Long id) {
        WmsWarehouseZone zone = baseMapper.selectById(id);
        if (zone != null) {
            List<WmsWarehouseLocation> locations = warehouseLocationMapper.selectList(new LambdaQueryWrapper<WmsWarehouseLocation>()
                    .eq(WmsWarehouseLocation::getZoneId, id)
                    .orderByAsc(WmsWarehouseLocation::getPickOrder, WmsWarehouseLocation::getId));
            zone.setLocations(locations);
        }
        return zone;
    }

    @Override
    public List<WmsWarehouseZone> selectWmsWarehouseZoneList(WmsWarehouseZone query) {
        LambdaQueryWrapper<WmsWarehouseZone> wrapper = new LambdaQueryWrapper<>();
        if (query != null) {
            if (query.getWarehouseId() != null) {
                wrapper.eq(WmsWarehouseZone::getWarehouseId, query.getWarehouseId());
            }
            if (StringUtils.isNotEmpty(query.getName())) {
                wrapper.like(WmsWarehouseZone::getName, query.getName());
            }
            if (StringUtils.isNotEmpty(query.getCode())) {
                wrapper.eq(WmsWarehouseZone::getCode, query.getCode());
            }
            if (query.getStatus() != null) {
                wrapper.eq(WmsWarehouseZone::getStatus, query.getStatus());
            }
            if (StringUtils.isNotEmpty(query.getZoneType())) {
                wrapper.eq(WmsWarehouseZone::getZoneType, query.getZoneType());
            }
        }
        wrapper.orderByAsc(WmsWarehouseZone::getPickOrder, WmsWarehouseZone::getId);
        return baseMapper.selectList(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertWmsWarehouseZone(WmsWarehouseZone entity) {
        fillCreateInfo(entity);
        return baseMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateWmsWarehouseZone(WmsWarehouseZone entity) {
        fillUpdateInfo(entity);
        return baseMapper.updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteWmsWarehouseZoneByIds(Long[] ids) {
        if (ids == null || ids.length == 0) {
            return 0;
        }
        for (Long id : ids) {
            removeLocations(id);
        }
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteWmsWarehouseZoneById(Long id) {
        removeLocations(id);
        return baseMapper.deleteById(id);
    }

    private void removeLocations(Long zoneId) {
        if (zoneId == null) {
            return;
        }
        warehouseLocationMapper.delete(new LambdaQueryWrapper<WmsWarehouseLocation>().eq(WmsWarehouseLocation::getZoneId, zoneId));
    }

    private void fillCreateInfo(WmsWarehouseZone entity) {
        Date now = DateUtils.getNowDate();
        String username = SecurityUtils.getUsername();
        if (entity.getStatus() == null) {
            entity.setStatus(1);
        }
        entity.setCreateName(username);
        entity.setCreateTime(now);
        entity.setModifyName(username);
        entity.setModifyTime(now);
    }

    private void fillUpdateInfo(WmsWarehouseZone entity) {
        entity.setModifyName(SecurityUtils.getUsername());
        entity.setModifyTime(DateUtils.getNowDate());
    }
}

